/**
 *   Joomdiet - components/assets/javascript/base.js
 *   Author: Juan Dapena Paz (juan@bittingbits.com)
 *   Copyright (C) 2010 Bitting Bits S.L.
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


window.addEvent('domready', function() {
    $$('.tablerow').addEvent('click', function() {
        $('infotable').empty();
        $('task').setProperty('value', 'loadinfotable');
        
        // creating result data
        //var textElement = document.createTextNode(this.id);
        //$('infopane').appendChild(textElement);
        $('cid').setProperty('value', this.id);
        $('form1').send({
            onComplete: function(response, responseXML)
            {
                var infotable = $('infotable');
                infotable.setHTML(response);  
                
                // set up info type selector
                var ingredientslink = $('ingredientslink');
                if (ingredientslink != null) {
                    ingredientslink.addEvent('click', function() {
                        $('task').setProperty('value', 'loadinfotable');
                        $('infotype').setProperty('value', 1);
                        $('form1').send({
                            onComplete: function(response, responseXML)
                            {
                                var infotable = $('infotable');
                                infotable.empty();
                                infotable.setHTML(response);
                            }
                        });
                    });
                }
                
                var nutrientslink = $('nutrientslink');
                if (nutrientslink != null) {
                    nutrientslink.addEvent('click', function() {
                        $('task').setProperty('value', 'loadinfotable');
                        $('infotype').setProperty('value', 2);
                        $('form1').send({
                            onComplete: function(response, responseXML)
                            {
                                var infotable = $('infotable');
                                infotable.empty();
                                infotable.setHTML(response);
                            }
                        });
                    });
                } 
            }
        });
        
        $$('.tablerow').removeClass('selected');
        this.addClass('selected');
    });
    
    var toamountform = $('toamountform');
    if (toamountform != null) {
        $('toamountform').addEvent('click', function() {
            $('infotable').empty();
            $('task').setProperty('value', 'loadamountform');
            $('form1').send({
                onComplete: function(response, responseXML)
                {
                    var infotable = $('infotable');
                    infotable.setHTML(response);   
                    
                    $('amountsubmit').addEvent('click', function() {
                        $('task').setProperty('value', 'loadinfotable');
                        $('amountvalue').setProperty('value', $('amountfield').get('value'));
                        $('form1').send({
                            onComplete: function(response, responseXML)
                            {
                                var infotable = $('infotable');
                                infotable.empty();
                                infotable.setHTML(response);
                            }
                        });
                    });
                    
                    
                }
            });
        });
     }
     
     // diet calibrator javascript
     if ($('daypartselectorarea') != null) {
        $$('.daypartitem').addEvent('click', function() {
            alert('groar');
        });
     }
});
